﻿using System;
using System.IO;

namespace FileIOApplication
{
    class Program
    {
        static void Main1(string[] args)
        {
            FileStream F = new FileStream("D:\\other\\test.dat",
            FileMode.OpenOrCreate, FileAccess.ReadWrite);

            for (int i = 1; i <= 20; i++)
            {
                F.WriteByte((byte)i);
            }

            F.Position = 0;

            for (int i = 0; i <= 20; i++)
            {
                Console.Write(F.ReadByte() + " ");
            }
            F.Close();
            Console.ReadKey();
        }

        static void Main2(string[] args)
        {
            try
            {
                using (StreamReader sr = new StreamReader("D:\\other\\Jamaica.txt", encoding: System.Text.Encoding.UTF8))
                {
                    string line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        Console.WriteLine(line);
                    }
                }
            }
            catch (Exception e)
            {
                // 向用户显示出错消息
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }
        }

        static void Main3(string[] args)
        {
            string[] names = new string[] { "ZARA Ali", "Nuha Ali" };
            using (StreamWriter sw = new StreamWriter("D:\\other\\names.txt"))
            {
                foreach (string s in names)
                {
                    sw.WriteLine(s);
                }
            }


            string line = "";
            using (StreamReader sr = new StreamReader("D:\\other\\names.txt"))
            { 
                while ((line = sr.ReadLine()) != null)
                {
                    Console.WriteLine(line);
                }
            }
        }
    }
}